package com.ecoolex.ying.space.api.graphql;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.io.HttpUtil;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.EstateProjectActivityCouponOrder;
import com.ecoolex.ying.space.entity.EstateProjectActivityCouponPackage;
import com.ecoolex.ying.space.enums.EstateProjectActivityCouponOrderStatus;
import com.ecoolex.ying.space.enums.EstateProjectActivityCouponPackageStatus;
import com.ecoolex.ying.space.pojo.SubmitEstateProjectActivityCouponOrderDTO;
import com.ecoolex.ying.space.ws.EstateProjectActivityCouponOrderService;
import com.ecoolex.ying.space.ws.EstateProjectActivityService;
import com.ecoolex.ying.space.ws.PaymentOrderService;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "地产项目活动代金券订单管理")
public class EstateProjectActivityCouponOrderGraphql {
	
	@Reference
	private EstateProjectActivityCouponOrderService orderService;
	
	@Reference
	private PaymentOrderService paymentOrderService;
	
	@Autowired
	private EstateProjectActivityService estateProjectActivityService;
	
	@TypeField(type = EstateProjectActivityCouponOrder.class,field = "canPay",description = "能发起支付")
	public boolean canPay(@Source EstateProjectActivityCouponOrder source) {
		return EstateProjectActivityCouponOrderStatus.canPay(source);
	}
	
	@TypeField(type = EstateProjectActivityCouponPackage.class,field = "canRefund",description = "能发起退款")
	public boolean canRefund(@Source EstateProjectActivityCouponPackage source) {
		return EstateProjectActivityCouponPackageStatus.canRefund(source);
	}
	
	@MutationField(field = "submitEstateProjectActivityCouponOrder", description = "提交地产项目活动代金券订单")
	public EstateProjectActivityCouponOrder submit(@NonNull SubmitEstateProjectActivityCouponOrderDTO submit,ConsumerMemberAuthority user) {
		user.assertLogin();
		submit.setUserId(user.userId());
		return orderService.submit(submit, user.userName());
	}
	
	@MutationField(field = "payEstateProjectActivityCouponOrder", description = "发起地产项目活动代金券订单支付")
	public Map payApply(@NonNull Integer orderId, HttpServletRequest request, ConsumerMemberAuthority user) {
		user.assertLogin();
		EstateProjectActivityCouponOrder order = orderService.getByOrderIdAndUserId(orderId, user.userId());
		Assert.isTrue(Check.notNull(order),ResultCode.VERIFY.build(1), "地产活动代金券订单不存在");
		return paymentOrderService.weiXinPayApply(order, HttpUtil.getIpAddr(request), user.userName());
	}
	
	@MutationField(field = "refundEstateProjectActivityCouponOrder", description = "申请地产项目活动代金券退款")
	public boolean refundApply(@NonNull Integer orderId,@NonNull String code, ConsumerMemberAuthority user) {
		user.assertLogin();
		EstateProjectActivityCouponOrder order = orderService.getByOrderIdAndUserId(orderId, user.userId());
		Assert.isTrue(Check.notNull(order),ResultCode.VERIFY.build(1), "地产活动代金券订单不存在");
		EstateProjectActivityCouponPackage couponPackage = estateProjectActivityService.getEstateProjectActivityCouponPackageByCode(code, user.userId());
		Assert.isTrue(Check.notNull(couponPackage),ResultCode.VERIFY.build(2), "地产活动用户代金券不存在");
		
		return paymentOrderService.weixinRefundApply(order,couponPackage, user.userName());
	}
}
